﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net.Mail;
using System.Windows.Forms;

namespace Antix.Mail.Tester
{
    public partial class MessageForm : Form
    {
        public MessageForm()
        {
            InitializeComponent();
        }

        void sendButton_Click(object sender, EventArgs e)
        {
            statusLabel.Text = "Attempting Send ...";
            statusStrip.Refresh();

            try
            {
                using (var client = new SmtpClient(
                    ServerControl.Text, int.Parse(PortControl.Text)))
                {
                    var message = new MailMessage
                    {
                        From = new MailAddress(fromTextBox.Text)
                    };

                    message.To.Add(toTextBox.Text);
                    message.Subject = subjectTextbox.Text;
                    message.Body = bodyTextbox.Text;

                    if (File.Exists(attachmentTextBox.Text))
                    {
                        message.Attachments.Add(
                            new Attachment(attachmentTextBox.Text)
                        );
                    }

                    client.Send(message);

                    statusLabel.Text = $"'{subjectTextbox.Text}' send on {DateTime.Now}";
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
                statusLabel.Text = ex.Message;
            }
        }

        void attachmentButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (attachmentFileDialog.ShowDialog() == DialogResult.OK)
                {
                    attachmentTextBox.Text = attachmentFileDialog.FileName;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.ToString());
                statusLabel.Text = ex.Message;
            }
        }
    }
}